﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;


namespace OpenAuth.App
{
    public class TriageSignApp : BaseApp<TriageSign>
    {
        private RevelanceManagerApp _revelanceApp;
        private OUTP_JZJLKApp outApp;
        private OUTP_FZ_YSDLXXApp fzApp;
       
        private IAuth _auth;

       

        //public void Add(AddOrUpdateTriageSignReq req)
        //{
        //    var obj = req.MapTo<TriageSign>();
        //    //todo:补充或调整自己需要的字段
        //    //obj.CreateTime = DateTime.Now;
        //    //var user = _auth.GetCurrentUser().User;
        //    //obj.CreateUserId = user.Id;
        //    //obj.CreateUserName = user.Name;
        //    Repository.Add(obj);
        //}
        public void Add(TriageSign model)
        {
            Repository.Add(model);
        }
        /// <summary>
        /// 医生端叫号之后更新本地库叫号次数和时间
        /// </summary>
        /// <param name="patid"></param>
        /// <param name="GHXH"></param>
        public void Update(string patid, string GHXH)
        {
            string date = DateTime.Now.ToString("yyyyMMdd");
            Repository.Update(u => u.PATID.ToString("0") == patid && u.GHHX == int.Parse(GHXH) && u.GHRQ.StartsWith(date), u => new TriageSign
            {
                JHCS = 1,               
                ZHJHSJ = DateTime.Now.ToString("yyyyMMddhh:mm:ss"),
                ZHJHSJ2 = DateTime.Now.ToLocalTime(),
                JLZT=1
                

            });
        }
        /// <summary>
        /// 获取本地表中签到记录
        /// </summary>
        /// <param name="patid"></param>
        /// <param name="GHXH"></param>
        /// <returns></returns>
        public TriageSign QuerySingle(string patid,string GHXH)
        {
           return Repository.FindSingle(u => u.PATID.ToString("0")==patid &&u.GHXH.ToString("0") == GHXH);
        }

        // public void Update(AddOrUpdateTriageSignReq obj)
        //{
        //    var user = _auth.GetCurrentUser().User;
        //    UnitWork.Update<TriageSign>(u => u.Id == obj.Id, u => new TriageSign
        //    {
        //        GHXH = obj.GHXH,
        //        JSSJH = obj.JSSJH,
        //        PATID = obj.PATID,
        //        BLH = obj.BLH,
        //        YBDM = obj.YBDM,
        //        CARDNO = obj.CARDNO,
        //        YYXH = obj.YYXH,
        //        HZXM = obj.HZXM,
        //        GHRQ = obj.GHRQ,
        //        KSDM = obj.KSDM,
        //        KSMC = obj.KSMC,
        //        YSDM = obj.YSDM,
        //        YSMC = obj.YSMC,
        //        GHHX = obj.GHHX,
        //        GHLB = obj.GHLB,
        //        IPDZ = obj.IPDZ,
        //        ZJDM = obj.ZJDM,
        //        ZJMC = obj.ZJMC,
        //        ZQDM = obj.ZQDM,
        //        SCJHSJ = obj.SCJHSJ,
        //        ZHJHSJ = obj.ZHJHSJ,
        //        JHCS = obj.JHCS,
        //        ZHJHSJ2 = obj.ZHJHSJ2,
        //        JLZT = obj.JLZT,
        //        //UpdateTime = DateTime.Now,
        //        //UpdateUserId = user.Id,
        //        //UpdateUserName = user.Name
        //        //todo:补充或调整自己需要的字段
        //    });

        //}
        /// <summary>
        /// 自助机端签到方法
        /// </summary>
        /// <param name="model"></param>
        public void AddSign(OUTP_JZJLK model)
        {
            var temp = fzApp.QuerySingle(model.YSDM);
            TriageSign ts = new TriageSign();
            ts.GHXH = model.GHXH;
            ts.JSSJH = model.JSSJH;
            ts.PATID = model.PATID;            
            ts.BLH = model.BLH;
            ts.YBDM = model.YBDM;
            ts.CARDNO = model.CARDNO;
            ts.YYXH = model.YYXH;
            ts.HZXM = model.HZXM;
            ts.GHRQ = model.GHRQ;
            ts.KSDM = model.KSDM;
            ts.KSMC = model.KSMC;
            ts.YSDM = model.YSDM;
            ts.YSMC = model.YSMC;
            ts.GHHX = model.GHHX;
            ts.GHLB = model.GHLB;
            ts.YYXH = model.YYXH;
            ts.IPDZ = temp.IPDZ;
            ts.ZJDM = temp.ZJDM;
            ts.ZJMC = temp.ZJMC;
            ts.ZQDM = model.ZQDM;
            ts.SCJHSJ = model.SCJZSJ;
            ts.ZHJHSJ = "";
            ts.JHCS = 1;
            ts.ZHJHSJ2 = DateTime.Now.ToLocalTime();
            ts.JLZT = 0;
            ts.XH = Decimal.Parse(RandomHelper.GenerateRandomCode(8));
            Add(ts);

        }
        /// <summary>
        /// 获取本地表中得挂号患者列表
        /// </summary>
        /// <param name="ysdm">医生代码</param>
        /// <param name="date">日期：20191206</param>
        /// <returns></returns>
        public List<TriageSign> QueryList(string ysdm, string date)
        {
            return Repository.Find(u => u.YSDM == ysdm && u.GHRQ.StartsWith(date)).ToList();
        }
        

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="unitWork"></param>
        /// <param name="repository"></param>
        /// <param name="app"></param>
        /// <param name="auth"></param>
        /// <param name="_outApp"></param>
        /// <param name="_fzApp"></param>
        public TriageSignApp(IUnitWork unitWork, IRepository<TriageSign> repository,
            RevelanceManagerApp app, IAuth auth,OUTP_JZJLKApp _outApp, OUTP_FZ_YSDLXXApp _fzApp) : base(unitWork, repository)
        {
            _revelanceApp = app;
            _auth = auth;
            outApp = _outApp;
            fzApp = _fzApp;
        }
        
    }
}